package com.ys.controller.api.storage;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.config.MenuId;
import com.ys.entity.storage.TccAllocateCancelDetail;
import com.ys.service.sys.UploadService;
import com.ys.util.admin.ConditionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.entity.amazonorder.TpAmazonShopOrder;
import com.ys.entity.storage.TccStockControlRunning;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tccStockControlRunning")
public class ApiInterTccStockControlRunningController {
	

	private static final Logger logger = LoggerFactory.getLogger(ApiInterTccStockControlRunningController.class);
	@Autowired
	private CommonApiService<TccStockControlRunning> commonApiService;

	//菜单id
	private Long menuId = MenuId.TCC_StockControlRunning;
	
	@Autowired
	private CommonApiService<TccStockControlRunning> commonApiServiceDetail;
//	@Autowired
//	private TccStockControlRunningService tpFbaPlanOrderService;

//	@RequiresPermissions("tpFbaPlanOrder:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute TccStockControlRunning cond,
			String conditions
			){
		
		cond.addField("B.VGsJc vGsJc,B.GDepot locationArea,B.VKq locationKq,B.VHw vHw,B.ProductCode code,B.ProductName name")
		    .setOrderby("A.createTime desc")
			.addLeftJoin("TCC_StockControl B on B.Guid = A.GuidStockControl")
			.addLeftJoin("TCC_Location C on C.Chw=A.locationHw")
			.addField("C.Vhw locationName");
		
		return commonApiService.searchLay(cond);

//		cond.addField("B.VGsJc vGsJc,B.GDepot locationArea,B.VKq locationKq,B.VHw vHw,B.ProductCode code,B.ProductName name")
//		    .setOrderby("A.createTime desc")
//			.addLeftJoin("TCC_StockControl B on B.Guid = A.GuidStockControl")
//			.addLeftJoin("TCC_Location C on C.Chw=A.locationHw")
//			.addField("C.Vhw locationName");
//
//		return commonApiService.searchLay(cond);


	}

	/**
	 * 设置查询的cond的sql设置
	 */
	private TccStockControlRunning setSeachSql(TccStockControlRunning cond){
		cond.addField("B.VGsJc vGsJc,B.GDepot locationArea,B.VKq locationKq,B.VHw vHw,B.ProductCode code,B.ProductName name")
				.setOrderby("A.createTime desc")
				.addLeftJoin("TCC_StockControl B on B.Guid = A.GuidStockControl")
				.addLeftJoin("TCC_Location C on C.Chw=A.locationHw")
				.addField("C.Vhw locationName");
		return  cond;
	}

	/**
	 * 导出
	 */
	@RequestMapping("/export")
	@ResponseBody
	public R export(@ModelAttribute TccStockControlRunning cond, String conditions){
		try {
			cond.setPage(0).setSize(999999);
			String _ys_createUserInfo = ConditionUtil.getConditionValue(conditions, "_ys_createUserInfo");
			if (_ys_createUserInfo != null && _ys_createUserInfo.length() != 0) {
				cond.where("t2.UserId LIKE '%" + _ys_createUserInfo + "%'" + " OR  t2.UserName LIKE '%" + _ys_createUserInfo + "%'");
			}

			cond = setSeachSql(cond);

			//获取菜单字段数据
			List<YsCol> listField = new YsCol()
					.setMenuId(menuId)
					.setFrontShow(1)
					.setOrderby("A.SortNo asc")
					.queryList();
			//获取数据
			List<Map<String, Object>> listData = cond.queryMap();
			//处理数据
			ExportExcelUtils<TccStockControlRunning> util = new ExportExcelUtils<>(new TccStockControlRunning());
			String toFilePath = util.exportExcel(listData, listField, "库存流水");
			UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
			return R.ok(dto.getUrl());
		}catch (Exception e){
			logger.error("导出出错：",e.getMessage(),e);
			return R.error("导出失败，请稍后重试！");
		}

	}
	
}
